import processor, re

processor.me.inventory = []

def getWeightHeld(self):
    weight = 0
    for o in self.inventory:
        weight += o.weight
    return weight
processor.transplant(getWeightHeld, processor.me)

def acquire(self, object):
    if object not in processor.objects: processor.objects.append(object)
    if object.container:
        object.container.remove(object)
    if object not in processor.accessibleObjects:
        processor.accessibleObjects.append(object)
    self.inventory.append(object)
    object.container = self.inventory
processor.transplant(acquire, processor.me)

def isMovable(self):
    return "weight" in self.__dict__
processor.Object.isMovable = isMovable

getRegexp = re.compile("^(g|get|pick up|take) (.*)")

class Get(object):
    def process(self, command):
        match = getRegexp.search(command)
        if match:
            i = processor.getItem(match.group(2))
            if i not in processor.me.inventory:
                if "weight" not in i.__dict__:
                    return "That isn't available."
                if i.weight > processor.me.maxweight:
                    return "It's too heavy to move."
                elif processor.me.getWeightHeld() + i.weight > processor.me.maxweight:
                    return "You can't carry any more."
                else:
                    processor.me.acquire(i)
                    return "You pick up " + i.nameAsKnown() + "."
            else:
                return "You're already holding " + i.nameAsKnown() + "."

processor.commands.append(Get())



dropAllRegexp = re.compile("^(d|drop|put down) (all|everything)$")
dropRegexp = re.compile("^(d|drop|put down) (.*)$")

class Drop:
    def process(self, command):
        dropAll = dropAllRegexp.search(command)
        drop = dropRegexp.search(command)
        if dropAll:
            if len(processor.me.inventory):
                inventory = processor.smartJoin([a.nameAsKnown() for a in processor.me.inventory])
                for i in processor.me.inventory: processor.me.room.acquire(i)
                return "You drop " + inventory  + "."
            else:
                return "You're not carrying anything."
        elif drop:
            i = processor.getItem(drop.group(2))
            if i in processor.me.inventory:
                processor.me.room.acquire(i)
                return "Dropped " + i.nameAsKnown() + "."
            else:
                return "You're not carrying it."

processor.commands.append(Drop())



inventoryRegexp = re.compile("^(i|inventory)$")

class Inventory:
    def process(self, command):
        match = inventoryRegexp.search(command)
        if match:
            if len(processor.me.inventory):
                return "You're carrying " \
                        + processor.smartJoin([a.nameAsUnknown() for a in processor.me.inventory]) + "."
            else:
                return "You're carrying nothing."

processor.commands.append(Inventory())

